package com.xzj.admin.analysis.base.custom.ct;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.ThemeSpecialAnalysisHandle;
import com.xzj.admin.analysis.base.custom.common.PlatformSpecialHandle;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: GzThemeSpecialAnalysisHandle
 * @description: 贵州特殊主题处理业务
 * @author: XiaZunJun
 * @create: 2024-11-18 17:14
 */
@Slf4j
public class GzThemeSpecialAnalysisHandle extends ThemeSpecialAnalysisHandle {

    private static final String VM_VINCE = "vm:vince";

    public GzThemeSpecialAnalysisHandle(String cloudId, String cloudType) {
        super(cloudId, cloudType,
                message ->
                        RESOURCE_TYPE.vm.equals(message.getMessageSource())
                                ||RESOURCE_TYPE.vnic.equals(message.getMessageSource())
                                ||RESOURCE_TYPE.datacenter.equals(message.getMessageSource())
                                ||RESOURCE_TYPE.host.equals(message.getMessageSource())
                                ||RESOURCE_TYPE.region.equals(message.getMessageSource())
        );
    }

    static {
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.datacenter.name(), MESSAGE_TAG.add.name()), PlatformSpecialHandle::datacenterAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.datacenter.name(), MESSAGE_TAG.edit.name()), PlatformSpecialHandle::datacenterAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.region.name(), MESSAGE_TAG.add.name()), PlatformSpecialHandle::regionAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.region.name(), MESSAGE_TAG.edit.name()), PlatformSpecialHandle::regionAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.host.name(), MESSAGE_TAG.add.name()), PlatformSpecialHandle::vmOrHostAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.host.name(), MESSAGE_TAG.edit.name()), PlatformSpecialHandle::vmOrHostAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.add.name()), GzThemeSpecialAnalysisHandle::vmAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.edit.name()), GzThemeSpecialAnalysisHandle::vmEdit);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.del.name()), GzThemeSpecialAnalysisHandle::vmDel);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vnic.name(), MESSAGE_TAG.add.name()), GzThemeSpecialAnalysisHandle::nicAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vnic.name(), MESSAGE_TAG.edit.name()), GzThemeSpecialAnalysisHandle::nicAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.add.name()), GzThemeSpecialAnalysisHandle::vmAdd);
        putThemeSpecialHandle(buildKey(RESOURCE_TYPE.vm.name(), MESSAGE_TAG.add.name()), GzThemeSpecialAnalysisHandle::vmAdd);
    }

    private static void vmAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject rowData = message.getRowData();
        String vmId = rowData.getJSONObject("basicInfo").getString("vmId");
        JSONArray netWorks = rowData.getJSONArray("netWorks");
        List<String> vinceList = new ArrayList<>();
        for(int i=0;i<netWorks.size();i++){
            String nicUuid = netWorks.getJSONObject(i).getString("nicUuid");
            vinceList.add(nicUuid);
            redisHandle.setCacheMapValue(buildKey(info.getCacheKeyPrefix(),VM_VINCE),nicUuid,vmId);
        }
        if(!vinceList.isEmpty()){
            String vmNicCacheKey = buildKey(info.getCacheKeyPrefix(),vmId);
            redisHandle.setCacheList(vmNicCacheKey,vinceList);
        }
        PlatformSpecialHandle.vmOrHostAdd(message,info);
    }
    private static void vmEdit(ResMessageManage message, CommonFlowInfo info){
        JSONObject rowData = message.getRowData();
        String vmId = rowData.getJSONObject("basicInfo").getString("vmId");
        JSONArray netWorks = rowData.getJSONArray("netWorks");
        List<String> vinceList = new ArrayList<>();
        for(int i=0;i<netWorks.size();i++){
            String nicUuid = netWorks.getJSONObject(i).getString("nicUuid");
            vinceList.add(nicUuid);
        }
        String vmNicCacheKey = buildKey(info.getCacheKeyPrefix(),vmId);
        // 获取缓存中的网卡列表
        List<String> cacheList = redisHandle.getCacheList(vmNicCacheKey);
        if(null!=cacheList){
            // 如果有网卡列表，先替换缓存中的数据
            if(!vinceList.isEmpty()){
                redisHandle.setCacheList(vmNicCacheKey,vinceList);
            }
            // 根据缓存中的网卡信息，删除缓存中不存在的网卡信息
            for(String nicUuid:cacheList){
                if(!vinceList.contains(nicUuid)){
                    redisHandle.deleteHashObject(buildKey(info.getCacheKeyPrefix(),VM_VINCE),nicUuid);
                }else{
                    vinceList.remove(nicUuid);
                }
            }
        }
        // 新增网卡信息
        for(String nicUuid:vinceList){
            redisHandle.setCacheMapValue(buildKey(info.getCacheKeyPrefix(),VM_VINCE),nicUuid,vmId);
        }
        PlatformSpecialHandle.vmOrHostAdd(message,info);
    }
    private static void vmDel(ResMessageManage message, CommonFlowInfo info){
        JSONObject rowData = message.getRowData();
        String vmId = rowData.getJSONObject("basicInfo").getString("vmId");
        String vmNicCacheKey = buildKey(info.getCacheKeyPrefix(),vmId);
        // 获取缓存中的网卡列表
        List<String> cacheList = redisHandle.getCacheList(vmNicCacheKey);
        if(null!=cacheList){
            // 根据缓存中的网卡信息，删除缓存中不存在的网卡信息
            for(String nicUuid:cacheList){
                redisHandle.deleteHashObject(buildKey(info.getCacheKeyPrefix(),VM_VINCE),nicUuid);
            }
        }
    }
    private static void nicAdd(ResMessageManage message, CommonFlowInfo info){
        JSONObject rowData = message.getRowData();
        rowData.put("vdeviceId", redisHandle.getCacheMapValue(buildKey(info.getCacheKeyPrefix(),VM_VINCE),rowData.getString("nicUuid")));
        message.setMessageJson(JSON.toJSONString(rowData));
    }


}
